package com.cmcc.volte.xdr.util;

import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Arrays;

public class MessageUtil {

	private static SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	/**
	 * byte,byte数组默认填充值(0xFF)
	 */
	public static final byte DEFAULT_BYTE = (byte) -1;

	/**
	 * 长度不够时填充字节,为空格(ascii 32)
	 */
	public static final byte WHITE_BYTE = (byte) 32;

	/**
	 * 将int转换为byte数组
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] toByteArray(double data, int length) {
		byte[] bytes = new byte[length];
		ByteBuffer.wrap(bytes).putDouble(length - 8, data);
		return bytes;
	}

	/**
	 * 将无符号long转换为byte数组,向下兼容int,short,byte
	 * 
	 * @param data
	 * @param length
	 * @return
	 */
	// XXX 注意!有符号的数字转换可能会出错
	public static byte[] toByteArray(long data, int length) {

		byte[] bufParam = new byte[length];
		bufParam[length - 1] = (byte) data;
		for (int i = 1; i < bufParam.length; i++) {
			bufParam[length - 1 - i] = (byte) (data >>> (8 * i));
		}
		return bufParam;
	}

	// TODO 等规则统一化确定是左补还是右补0
	/**
	 * 字符串转换为byte数组
	 * 
	 * @param data
	 * @param length
	 * @return
	 */
	public static byte[] toByteArray(String data, int length) {
		byte[] bytes = new byte[length];
		// 数组总长-数据占字节数 = 需要左补偏移量
		byte[] strBytes = data.getBytes();
		ByteBuffer buffer = ByteBuffer.wrap(bytes);

		int offset = length - strBytes.length;
		if (offset < 0) {
			throw new IllegalArgumentException("数据超出规定长度,数据内容:" + data);
		} else if (offset > 0) {// 数据不足以填充byte[]需要补空白字符
			buffer.put(fillByteArray(offset, WHITE_BYTE));
		}
		buffer.put(strBytes);
		return bytes;
	}

	/**
	 * 根据ip字符串返回byte数组,如果长度不够会在头部之前插入0xFF
	 * 
	 * @param data
	 * @param length
	 * @return
	 */
	public static byte[] toByteArrayFromIP(String data, int length) {
		ByteBuffer buffer = ByteBuffer.allocate(length);
		byte[] temp = null;
		try {
			InetAddress address = InetAddress.getByName(data);
			temp = address.getAddress();
		} catch (UnknownHostException e) {
			throw new RuntimeException(e.getMessage());
		}
		int offset = length - temp.length;
		// 长度不足部分在头部之前插入0xFF
		if (offset > 0) {
			buffer.put(fillByteArray(offset, DEFAULT_BYTE));
		}
		buffer.put(temp);

		return buffer.array();

	}

	/**
	 * 字节数组转换为double
	 * 
	 * @param data
	 * @return
	 */
	public static double toDouble(byte[] data) {
		return ByteBuffer.wrap(data).getDouble();
	}

	/**
	 * 将无符号数字的byte数据转换为BigInteger
	 * 
	 * @param arr
	 * @return
	 */
	// XXX 注意!有符号的数字转换可能会出错
	public static BigInteger toBigInteger(byte[] arr) {
		// 在原数组前插入一字节作为符号位
		byte[] result = new byte[arr.length + 1];
		for (int i = arr.length; i > 0; i--) {
			result[i] = arr[i - 1];
		}
		return new BigInteger(result);

	}

	public static String formatDateTime(Object date) {
		return format.format(date);
	}

	public static String toIpAddress(byte[] bytes) {
		if (bytes[0] == -1 && bytes.length > 4) {
			bytes = Arrays.copyOfRange(bytes, bytes.length - 4, bytes.length);
		}
		try {
			return InetAddress.getByAddress(bytes).getHostAddress();
		} catch (UnknownHostException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 返回指定长度的数组,数组填充内容为0xFF
	 * 
	 * @param length
	 * @return
	 */
	public static byte[] fillByteArray(int length) {
		return fillByteArray(length, DEFAULT_BYTE);
	}

	/**
	 * 返回指定长度的数组,数组填充内容为fillByte
	 * 
	 * @param length
	 * @param fillByte
	 * @return
	 */
	public static byte[] fillByteArray(int length, byte fillByte) {
		byte[] temp = new byte[length];
		Arrays.fill(temp, fillByte);
		return temp;
	}

}